﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RepositoriesInterfaces;
using Entities;
using Subfaculty.ViewModels;


namespace Subfaculty.Controllers
{
    public class StudyPlanController : Controller
    {
        IStudyPlanItemRepository PlanItemRepository;
        IDirectionRepository DirectionRepository;
        IDisciplineRepository DisciplineRepository;
        IDepartmentRepository DepartmentRepository;

        public StudyPlanController(IStudyPlanItemRepository PlanItemRepository, IDirectionRepository DirectionRepository,
                                   IDisciplineRepository DisciplineRepository, IDepartmentRepository DepartmentRepository)
        {
            this.PlanItemRepository = PlanItemRepository;
            this.DirectionRepository = DirectionRepository;
            this.DisciplineRepository = DisciplineRepository;
            this.DepartmentRepository = DepartmentRepository;
        }

        // Show the StadyPlanItem's list
        public ActionResult Index(int id)
        {
            int currentDirectionId = GetCurrentDirectionId(id);
            var model = new StudyPlanViewModel
                            {
                                StudyPlanItems = PlanItemRepository.GetByDirectionId(currentDirectionId),
                                curentDirection = currentDirectionId
                            };
            ViewData["curentDirection"] = GetDirectionList(currentDirectionId);            
            return View(model);
        }

        // Create new instance of the StadyPlanItem
        // id - id of current diretion
        [HttpGet]
        public ActionResult Create(int id)
        {
            StudyPlanItem PlanItem = new StudyPlanItem();
            PlanItem.Direction = DirectionRepository.GetByID(id);
            ViewData["DisciplineId"] = GetDisciplinesList(PlanItem.Discipline.Id);
            ViewData["DepartmentId"] = GetDepartmentsList(PlanItem.Department.Id);
            var model = new StudyPlanEditViewModel
                            {
                                StudyPlan = PlanItem
                            };
            return View(model);
        }
        
        [HttpPost]
        public ActionResult Create(StudyPlanEditViewModel obj)
        {
            if (ModelState.IsValid)
            {
                SaveStudyPlanItem(obj);
                return RedirectToAction(String.Format("Index/{0}", obj.DirectionId));
            }
            else
            {
                ViewData["DisciplineId"] = GetDisciplinesList(obj.DisciplineId);
                ViewData["DepartmentId"] = GetDepartmentsList(obj.DepartmentId);
                obj.StudyPlan.Direction = DirectionRepository.GetByID(obj.DirectionId); // не биндится. Посмотреть!!!
                return View(obj);
            }
        }

        // Edit existed instance of the StadyPlanItem
        [HttpGet]
        public ActionResult Edit(int id)
        {
            StudyPlanItem PlanItem = PlanItemRepository.GetByID(id);
            ViewData["DisciplineId"] = GetDisciplinesList(PlanItem.Discipline.Id);
            ViewData["DepartmentId"] = GetDepartmentsList(PlanItem.Department.Id);
            var model = new StudyPlanEditViewModel  
                            {
                                StudyPlan = PlanItem             
                            };
            
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(StudyPlanEditViewModel obj)
        {
            if (ModelState.IsValid)
            {
                UpdateStudyPlanItem(obj);
                return RedirectToAction(String.Format("Index/{0}", obj.DirectionId));
            }
            else
            {
                ViewData["DisciplineId"] = GetDisciplinesList(obj.DisciplineId);
                ViewData["DepartmentId"] = GetDepartmentsList(obj.DepartmentId);
                obj.StudyPlan.Direction = DirectionRepository.GetByID(obj.DirectionId);
                return View(obj);
            }            
        }

        // Delete the instance of the StadyPlanItem
        [HttpGet]
        public ActionResult Delete(int id)
        {            
            StudyPlanItem PlanItem = PlanItemRepository.GetByID(id);
            int directionId = PlanItem.Direction.Id;
            PlanItemRepository.Delete(PlanItem);
           
            return RedirectToAction(String.Format("Index/{0}", directionId));
        }
        

        // --------------PRIVATE FUNCTIONS--------------

        private int GetCurrentDirectionId(int id)
        {
            IList<Direction> DirectionsList = DirectionRepository.GetAll();
            int curId = id;
            if ((id == 0) && (DirectionsList.Count != 0))
            {
                curId = DirectionsList[0].Id;
            }
            return curId;
        }

        private void UpdateStudyPlanItem(StudyPlanEditViewModel newData)
        {
            StudyPlanItem UpdatedItem = PlanItemRepository.GetByID(newData.StudyPlan.Id);
            StudyPlanItem NewPlanItem = newData.StudyPlan;            
            Department department = DepartmentRepository.GetByID(newData.DepartmentId);
            Discipline discipline = DisciplineRepository.GetByID(newData.DisciplineId);
            UpdatedItem.Department.StudyPlanItems.Remove(UpdatedItem);
            UpdatedItem.Discipline.StudyPlanItems.Remove(UpdatedItem);
            UpdatedItem.HoursLaboratory = NewPlanItem.HoursLaboratory;
            UpdatedItem.HoursLecture = NewPlanItem.HoursLecture;
            UpdatedItem.HoursPractice = NewPlanItem.HoursPractice;
            UpdatedItem.HoursSRS = NewPlanItem.HoursSRS;
            UpdatedItem.TypeSRS = NewPlanItem.TypeSRS;
            UpdatedItem.Semestr = NewPlanItem.Semestr;
            UpdatedItem.ControlType = NewPlanItem.ControlType;
            department.AddPlanItem(UpdatedItem);
            discipline.AddPlanItem(UpdatedItem);

            PlanItemRepository.SaveOrUpdate(UpdatedItem);
        }

        private void SaveStudyPlanItem(StudyPlanEditViewModel newData)
        {
            StudyPlanItem NewPlanItem = newData.StudyPlan;

            DirectionRepository.GetByID(newData.DirectionId).AddPlanItem(NewPlanItem);
            DepartmentRepository.GetByID(newData.DepartmentId).AddPlanItem(NewPlanItem);
            DisciplineRepository.GetByID(newData.DisciplineId).AddPlanItem(NewPlanItem);

            PlanItemRepository.Save(NewPlanItem);
        }

        private IEnumerable<SelectListItem> GetDepartmentsList(int idSelectedDepartment)
        {
            return DepartmentRepository.GetAll()
                                .Select(department => new SelectListItem
                                {
                                    Selected = (department.Id == idSelectedDepartment),
                                    Text = department.DepartmentTitle,
                                    Value = department.Id.ToString()
                                });
        }

        private IEnumerable<SelectListItem> GetDisciplinesList(int idSelectedDiscipline)
        {
            return DisciplineRepository.GetAll()
                                .Select(discipline => new SelectListItem
                                {
                                    Selected = (discipline.Id == idSelectedDiscipline),
                                    Text = discipline.DisciplineTitle,
                                    Value = discipline.Id.ToString()
                                });
        }

        private IEnumerable<SelectListItem> GetDirectionList(int idSelectedDirection)
        {
            return DirectionRepository.GetAll()
                                .Select(direction => new SelectListItem
                                {
                                    Selected = (direction.Id == idSelectedDirection),
                                    Text = direction.DirectionTitle,
                                    Value = direction.Id.ToString()
                                });
        }
    }
}
